<?php

/**
 * payments actions.
 *
 * @package    academia_de_escritores
 * @subpackage payments
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class paymentsActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $PaymentStatus=PaymentStatusTable::getInstance()->findOneByCode($request->getParameter("status_code"));
    $this->forward404Unless($PaymentStatus,"Page not found");
    $q=InvoicePaymentTable::getInstance()
    ->createQuery(InvoicePaymentTable::SHORT_ALIAS)
    ->innerJoin(InvoicePaymentTable::SHORT_ALIAS.".UserRegister")
    ->innerJoin(InvoicePaymentTable::SHORT_ALIAS.".PaymentType");
    $q->andWhere(InvoicePaymentTable::SHORT_ALIAS.".status_id=?",$PaymentStatus->getId());
    $this->Payments=$q->execute();
   
  }
  public function executeDetail(sfWebRequest $r){
  	$Payment=InvoicePaymentTable::getInstance()->findOneById($r->getParameter("id"));
  	$this->forward404Unless($Payment,"Payment not fount");
  	$PaymentStatus=$Payment->getPaymentStatus();
  	
  	$this->Payment=$Payment;
  	$this->buildFormApproval($Payment);
  	if($r->isMethod(sfWebRequest::POST)){
  		if($this->proccessForm($r, $this->formApproval)){
  			return $this->renderText("<script type='text/javascript' >window.location.href=window.location.href;</script>");
  		}
  	}
  }
  
  public function buildFormApproval(InvoicePayment $Payment){
  	try{
  		$this->formApproval=new PaymentApprovalDepositForm($Payment);
  	}catch(Exception $e){
  		$this->formApproval=$e->getMessage();
  	}
  }
  
  protected function proccessForm(sfWebRequest $request, sfForm $form){
	  $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
	    if ($form->isValid())
	    {
	      $payment = $form->save();
	      return true;
	    }else{
	    	return false;
	    }
  }
}
